package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.AssetsImages;
import com.zygh.policeasset.service.AssetsImagesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/uploadimages"})
@Api(tags = {"图片上传模块"})
@RestController
public class AssetsImagesController {
    @Autowired
    AssetsImagesService assetsImagesService;

    @RequestMapping(value = {"/findbypuid"}, method = {RequestMethod.GET})
    @ApiOperation("根据资产ID查询")
    public Object findById(@RequestParam Integer assetId) {
        return new CommonResult().success(this.assetsImagesService.findByAssetsId(assetId));
    }

    @RequestMapping(value = {"/image"}, method = {RequestMethod.POST})
    @ApiOperation("添加图片")
    public Object image(AssetsImages assetsImages) {
        if (this.assetsImagesService.add(assetsImages).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加失败");
    }

    @RequestMapping(value = {"/getImageUrl"}, method = {RequestMethod.POST})
    @ApiOperation("添加图片")
    public Object getImageUrl(AssetsImages assetsImages) {
        return assetsImagesService.getImageUrl(assetsImages);
    }

    @RequestMapping(value = {"/remove"}, method = {RequestMethod.POST})
    @ApiOperation("删除图片")
    public Object remove(@RequestBody JSONObject json) {
        if (StrUtil.isEmpty(String.valueOf(json.getInteger("id")))) {
            return new CommonResult().validateFailed("资产id不能为空");
        }
        if (this.assetsImagesService.delete(json.getInteger("id")).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }
}